#include <iostream>
#include <sw/redis++/redis++.h>
#include <vector>
#include <string>
#include "PPrint.hpp"
#include <chrono>
#include <set>

using std::cout;
using std::endl;
using std::string;
using std::unordered_map;
using std::vector;

using std::set;
using sw::redis::Redis;
using namespace std::chrono_literals;

void test1(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "xiaomi", 100);
    redis.zadd("key", {std::make_pair("huawei", 99),
                       std::make_pair("oppo", 98)});
    vector<std::pair<string, double>> members = {
        std::make_pair("vivo", 97),
        std::make_pair("apple", 96)};
    redis.zadd("key", members.begin(), members.end());

    vector<string> membersResults;
    auto it1 = std::back_inserter(membersResults);
    redis.zrange("key", 0, -1, it1);
    printContainer(membersResults);

    vector<std::pair<string, double>> memberswithscore;
    auto it2 = std::back_inserter(memberswithscore);
    redis.zrange("key", 0, -1, it2);
    printContainerPair(memberswithscore);
}

void test2(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "xiaomi", 100);
    redis.zadd("key", {std::make_pair("huawei", 99),
                       std::make_pair("oppo", 98)});
    vector<std::pair<string, double>> members = {
        std::make_pair("vivo", 97),
        std::make_pair("apple", 96)};
    redis.zadd("key", members.begin(), members.end());

    auto result = redis.zcard("key");
    cout << result << endl;
}

void test3(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "xiaomi", 100);
    redis.zadd("key", {std::make_pair("huawei", 99),
                       std::make_pair("oppo", 98)});
    vector<std::pair<string, double>> members = {
        std::make_pair("vivo", 97),
        std::make_pair("apple", 96)};
    redis.zadd("key", members.begin(), members.end());
    cout << redis.zcard("key") << endl;

    redis.zrem("key", "huawei");
    cout << redis.zcard("key") << endl;
}

void test4(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "xiaomi", 100);
    redis.zadd("key", {std::make_pair("huawei", 99),
                       std::make_pair("oppo", 98)});
    vector<std::pair<string, double>> members = {
        std::make_pair("vivo", 97),
        std::make_pair("apple", 96)};
    redis.zadd("key", members.begin(), members.end());

    auto score = redis.zscore("key", "xiaomi");
    if (score)
        cout << score.value() << endl;
    else
        cout << "score no value" << endl;
}

void test5(Redis &redis)
{
    cout << "zadd and zrange" << endl;
    redis.flushall();

    redis.zadd("key", "xiaomi", 100);
    redis.zadd("key", {std::make_pair("huawei", 99),
                       std::make_pair("oppo", 98)});
    vector<std::pair<string, double>> members = {
        std::make_pair("vivo", 97),
        std::make_pair("apple", 96)};
    redis.zadd("key", members.begin(), members.end());

    auto rank = redis.zrank("key", "xiaomi");
    if (rank)
        cout << rank.value() << endl;
    else
        cout << "rank no value" << endl;
}

int main()
{
    Redis redis("tcp://127.0.0.1:6379");

    test5(redis);
}